var mescenterObj = {
    flage: 0,
    msgType: "1",
    pageNum: "0",
    otherId:"",
    init: function () {
        publicObj.getIosAndData();
        $(".lefttip").hide();
        $(".righttip").hide();
        this.getMsgInfoCount();
        this.getMsgInfo();
        this.loadMore();
    },
    getMsgInfoCount: function () {
        publicObj.showLoad();
        var reqType = "POST";
        var isAsync = true;
        var dataType = "json";
        var reqUrl = ruquireObj.urlPrefix + 'MessageService/selectDeptCountOfMessage';
        var reqBody = {};
        reqBody.userId = sessionStorage.getItem("sUserId");
        publicObj.myAjax(this, this.getMsgInfoCountRe, reqUrl, reqBody, reqType, isAsync, dataType);
    },
    getMsgInfoCountRe: function (res, obj) {
        var data = res.response;
        var businessCount = data.businessCount;
        var systemCount = data.systemCount;
        if (businessCount > 0) {
            $(".lefttip").show();
        }
        if (systemCount > 0) {
            $(".righttip").show();
        }
        publicObj.hideLoad();
    },
    loadMore: function () {
        var me = this;
        $(window).scroll(function () {
            if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
                if (me.flage == 0) {
                    me.pageNum = (Number(me.pageNum) + 1).toString();
                    me.getMsgInfo();
                } else {
                    publicObj.myalert("没有更多啦~~")
                }

            }
        });
    },
    getMsgInfo: function () {
        publicObj.showLoad();
        publicObj.showLoad();
        var reqType = "POST";
        var isAsync = true;
        var dataType = "json";
        var reqUrl = ruquireObj.urlPrefix + 'MessageService/selectdeptmsg';
        var reqBody = {};
        reqBody.userId = sessionStorage.getItem("sUserId");
        reqBody.msgType = this.msgType;
        reqBody.pageNum = this.pageNum;
        publicObj.myAjax(this, this.getMsgInfoRe, reqUrl, reqBody, reqType, isAsync, dataType);
    },
    getMsgInfoRe: function (res, obj) {
        var data = res.response;
        var messageList = data.messageList;
        if (messageList.length == 0) {
            obj.flage = 1;
        }
        console.log(messageList);
        var reHtml = "";
        for (var i = 0; i < messageList.length; i++) {
            var nHtml = obj.getModel();
            nHtml = nHtml.replaceAll("{msgStr}", messageList[i].msgStr);
            nHtml = nHtml.replaceAll("{createTime}", messageList[i].createTime);
            nHtml = nHtml.replaceAll("{msgId}", messageList[i].msgId);
            nHtml = nHtml.replaceAll("{otherid}", messageList[i].otherId);
            if (messageList[i].msgStatus == "1") {
                nHtml = nHtml.replaceAll("{haveread}", "haveread");
            } else {
                nHtml = nHtml.replaceAll("{haveread}", "");
            }
            reHtml += nHtml;
        }
        $(".meslistbox").append(reHtml);
        obj.bingEvent(obj);
        publicObj.hideLoad();
    },
    bingEvent: function (obj) {

        $(".left").unbind("click").bind("click", function (e) {
            $(".left").removeClass("activenav");
            $(".right").removeClass("activenav");
            $(".left").addClass("activenav");
            obj.flage = 0;
            obj.msgType = "1";
            obj.pageNum = "0";
            $(".meslistbox").html('');
            obj.getMsgInfo();
        });

        $(".right").unbind("click").bind("click", function (e) {
            $(".left").removeClass("activenav");
            $(".right").removeClass("activenav");
            $(".right").addClass("activenav");
            obj.flage = 0;
            obj.msgType = "2";
            obj.pageNum = "0";
            $(".meslistbox").html('');
            obj.getMsgInfo();
        });

        $(".list").each(function () {
            var me = this;
            $(this).unbind("click").bind('click', function (e) {
                publicObj.showLoad();
                var reqType = "POST";
                var isAsync = true;
                var dataType = "json";
                var reqUrl = ruquireObj.urlPrefix + 'MessageService/updateMessage';
                var reqBody = {};
                reqBody.msgId = $(this).attr("msgId");
                var otherid = $(this).attr("otherid");
                obj.otherId = otherid;
                console.log(otherid)
                publicObj.myAjax(obj, obj.updateMessageRe, reqUrl, reqBody, reqType, isAsync, dataType);
            })
        });
    },
    updateMessageRe: function (res, obj) {

        if (obj.msgType == 1) {
            console.log(obj.otherId)
            location.href = "./transaction.html?oriderid="+obj.otherId;
        } else {
            obj.getMsgInfo(obj.msgType, "0");
            obj.getMsgInfoCount();
        }
    },
    getModel: function () {
        var Html = "<div class=\"list {haveread} \" msgId='{msgId}' otherid = '{otherid}'>\n" +
            "            <div class=\"txt\">{msgStr}</div>\n" +
            "            <div class=\"time\">{createTime}</div>\n" +
            "        </div>";
        return Html;
    },

};
mescenterObj.init();